package com.bsj.pay.controller.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsj.pay.common.Result;
import com.bsj.pay.model.PartnerBean;
import com.bsj.pay.service.LogService;
import com.bsj.pay.service.PartnerService;
import com.bsj.pay.util.ResultUtil;
import com.bsj.pay.util.security.MD5;
import com.bsj.pay.util.security.PassWrodsCreater;
import com.github.pagehelper.PageInfo;

@Controller("adminPartnerController")
@RequestMapping("/admin/partner")
public class PartnerController {
	
	private static final Logger log = LogManager.getLogger(PartnerController.class);
	
	@Autowired
	private PartnerService partnerService;
	
	@Autowired
	private LogService logService;
	
	@ResponseBody
	@RequestMapping(value = "/list.html")
	public Map list(HttpServletRequest request, HttpServletResponse res,int page,int limit,PartnerBean partner) {
		PartnerBean queryModel=new PartnerBean();
		if(!StringUtils.isEmpty(partner.getName()))queryModel.setName(partner.getName());
		if(partner.getStatus()!=null)queryModel.setStatus(partner.getStatus());
		PageInfo<PartnerBean> pageInfo=partnerService.pageList(queryModel, page, limit);
		return ResultUtil.getQuerySuccessResult(pageInfo);
	}
	
	
	@RequestMapping("/listPage.html")
    public ModelAndView listPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("admin/partner/list");
		return mav;
    }
	
	@RequestMapping("/addPage.html")
    public ModelAndView addPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("admin/partner/add");
		return mav;
    }
	
	@RequestMapping("/updateQuotaPage.html")
    public ModelAndView updateQuotaPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav,Long id) {
		mav.addObject("partner", partnerService.getById(id));
		mav.setViewName("admin/partner/updateQuota");
		return mav;
    }
	
	@ResponseBody
	@RequestMapping("/updateQuota.html")
	public Result updateQuota(HttpServletRequest request, HttpServletResponse res,Long id,int type,int amount) {
		Result result=new Result();
		try {
			if(type==1)
			{
				partnerService.addQuota(id, amount,"手动变更");
				result.setCode(0);
				result.setMsg("操作成功");
			}
			else if(type==-1)
			{
				PartnerBean partner=partnerService.getById(id);
				if(partner.getQuota()-amount<0)
				{
					result.setCode(1);
					result.setMsg("额度不能小于0");
				}
				else
				{
					partnerService.subQuota(id, amount,"手动变更");
					result.setCode(0);
					result.setMsg("操作成功");
				}
			}
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode(500);
			result.setMsg(e.getMessage());
			return result;
		}

	}
	
	@ResponseBody
	@RequestMapping("/remove.html")
	public Result remove(HttpServletRequest request, HttpServletResponse res,Long id) {
		Result result=new Result();
		try {
			if(partnerService.removeById(id)>0)
			{
				result.setCode(0);
				result.setMsg("删除成功");
			}
			else
			{
				result.setCode(1);
				result.setMsg("删除失败");
			}
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode(500);
			result.setMsg(e.getMessage());
			return result;
		}

	}
	
	
	@ResponseBody
	@RequestMapping("/add.html")
	public Result add(HttpServletRequest request,HttpServletResponse response,ModelAndView mav,PartnerBean partner)
	{
		Result result=new Result();
		try {
			String password=PassWrodsCreater.generatePassword(6);
			partner.setCharge_time_out(60*60*24);
			partner.setPassword(MD5.getMD5(password));
			partner.setApi_key(MD5.getMD5(String.valueOf(System.currentTimeMillis()/1000)));
			partner.setCreate_time(System.currentTimeMillis()/1000);
			partner.setUpdate_time(System.currentTimeMillis()/1000);
			partnerService.insert(partner);
			result.setCode(0);
			result.setMsg("添加成功,密码为"+password);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode(1);
			result.setMsg(e.getMessage());
			return result;
		}
		
	}
	
	
	@RequestMapping("/updatePage.html")
	public ModelAndView updatePage(HttpServletRequest request, HttpServletResponse response, HttpSession session,ModelAndView mav,Long id) {
		mav.addObject("partner", partnerService.getById(id));
		mav.setViewName("admin/partner/update");
		return mav;
	}
	
	
	@ResponseBody
	@RequestMapping("/updateStatus.html")
	public Result updateStatus(HttpServletRequest request, HttpServletResponse res,Long id,Integer status) {
		Result result=new Result();
		try {
			PartnerBean partner=new PartnerBean();
			partner.setId(id);
			partner.setStatus(status==0?1:0);
			if(partnerService.update(partner)>0)
			{
				result.setCode(0);
				result.setMsg("修改成功");
			}
			else
			{
				result.setCode(1);
				result.setMsg("修改失败");
			}
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode(500);
			result.setMsg(e.getMessage());
			return result;
		}

	}
	
	@ResponseBody
	@RequestMapping("/resetPassword.html")
	public Result resetPassword(HttpServletRequest request,HttpServletResponse response,Long id){
		Result result=new Result();
		PartnerBean partner = new PartnerBean();
		partner.setId(id);
		String password=PassWrodsCreater.generatePassword(6);
		partner.setPassword(MD5.getMD5(password));
		partnerService.update(partner);
		logService.addLog(request, "重置合作商密码,ID:"+id);
		result.setCode(0);
		result.setMsg("重置成功,新密码为"+password);
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/update.html")
	public Result update(HttpServletRequest request, HttpServletResponse res,PartnerBean partner) {
		Result result=new Result();
		try {
			partner.setUpdate_time(System.currentTimeMillis()/1000);
			partnerService.update(partner);
			result.setCode(0);
			result.setMsg("修改成功");
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode(500);
			result.setMsg(e.getMessage());
			return result;
		}

	}
	
	@ResponseBody
	@RequestMapping(value = "/updateDispatchOrderStatus.html")
	public Result updateDispatchOrderStatus(HttpServletRequest req, HttpServletResponse res,Long id,Integer dispatch_order_status) {
		Result result=new Result();
		try {
			PartnerBean partner=new PartnerBean();
			partner.setId(id);
			partner.setDispatch_order_status(dispatch_order_status==0?1:0);
			if(partnerService.update(partner)>0)
			{
				result.setCode(0);
				result.setMsg("修改成功");
			}
			else
			{
				result.setCode(1);
				result.setMsg("修改失败");
			}
			
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode(500);
			result.setMsg(e.getMessage());
			return result;
		}

	}
}
